package com.watson.juc.testlist;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author watson
 * @date 2021/7/30 10:51
 * @Description: TODO
 **/
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        Runnable barrierAction;
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("召唤神龙成功");
        });
        for (int i = 1; i <= 7; i++) {
            final int temp = i;
            new Thread(() -> {
                System.out.println("收集到" + temp + "颗龙珠");
                try {
                    //等待，  如果上面是8 ，会一直阻塞
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

            }, String.valueOf(i)).start();
        }
    }
}
